/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com, guwei8576431@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 *
 * Created on 2009-11-06
 *******************************************************************************/

package org.cgtw.scriptprofile.widget;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.text.View;

public class ProfileMainTabbedPaneUI extends BasicTabbedPaneUI {

	private static Color defaultColor = Color.white;

	private static Color selectColor = new Color(214,207,239);

	protected int minTabWidth = 20;

	public ProfileMainTabbedPaneUI() {

	}

	public static ComponentUI createUI(JComponent x) {
		return new ProfileMainTabbedPaneUI();
	}

	protected int calculateTabHeight(int tabPlacement, int tabIndex,
			int fontHeight) {
		if (tabPlacement != JTabbedPane.LEFT
				&& tabPlacement != JTabbedPane.RIGHT)
			return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
		else {
			return 60;
		}
	}

	protected int calculateTabWidth(int tabPlacement, int tabIndex,
			FontMetrics metrics) {
		if (tabPlacement != JTabbedPane.LEFT
				&& tabPlacement != JTabbedPane.RIGHT)
			return super.calculateTabWidth(tabPlacement, tabIndex, metrics);
		else {
			return 120;
		}
	}



	@Override
	protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex, Rectangle iconRect, Rectangle textRect, boolean isSelected) {
		// TODO Auto-generated method stub
//		super.paintFocusIndicator(g, tabPlacement, rects, tabIndex, iconRect, textRect,
//				isSelected);
	}

	@Override
	protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
			int x, int y, int w, int h, boolean isSelected) {
		if (tabPlacement == LEFT) {
			int bottom = y + (h - 1);
			int right = x + (w - 1);
			paintUILeftTabBorder(tabIndex, g, x, y, w, h, bottom, right,
					isSelected);
		} else {
			super.paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h,
					isSelected);
		}
	}



	@Override
	protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
		// TODO Auto-generated method stub
		super.paintContentBorder(g, tabPlacement, selectedIndex);
	}

	private void paintUILeftTabBorder(int tabIndex, Graphics g, int x, int y, int w, int h, int bottom, int right, boolean isSelected) {
		if(isSelected){
			g.setColor(selectColor);
			g.fillRect(x + 2, y + 2, w - 4, h - 4);
		}else{
			g.setColor(defaultColor);
			g.fillRect(x, y, w, h);
		}

	}

	protected void layoutLabel(int tabPlacement, FontMetrics metrics,
			int tabIndex, String title, Icon icon, Rectangle tabRect,
			Rectangle iconRect, Rectangle textRect, boolean isSelected) {

		if (tabPlacement != JTabbedPane.LEFT
				&& tabPlacement != JTabbedPane.RIGHT) {
			super.layoutLabel(tabPlacement, metrics, tabIndex, title, icon,
					tabRect, iconRect, textRect, isSelected);
			return;
		}

		textRect.x = textRect.y = iconRect.x = iconRect.y = 0;

		View v = getTextViewForTab(tabIndex);
		if (v != null) {
			tabPane.putClientProperty("html", v);
		}

		SwingUtilities.layoutCompoundLabel((JComponent) tabPane, metrics,
				title, icon, SwingUtilities.CENTER, SwingUtilities.CENTER,
				SwingUtilities.CENTER, SwingUtilities.TRAILING, tabRect,
				iconRect, textRect, textIconGap);

		tabPane.putClientProperty("html", null);

		textRect.height = metrics.stringWidth(title);
		textRect.width = metrics.getHeight();
		if(icon!=null){
			iconRect.width = icon.getIconHeight();
			iconRect.height = icon.getIconWidth();
		}else{
			iconRect.width = 0;
			iconRect.height = 0;
		}

		iconRect.x = (tabRect.width - iconRect.width) / 2 + tabRect.x;
		iconRect.y = (tabRect.height - iconRect.height) / 3 + tabRect.y;

		textRect.x = (tabRect.width - textRect.height) / 2 + tabRect.x;
		textRect.y = iconRect.y + iconRect.height + textRect.width / 2
				+ textIconGap;

	}

	protected void paintIcon(Graphics g, int tabPlacement, int tabIndex,
			Icon icon, Rectangle iconRect, boolean isSelected) {
		if(icon==null){
			return;
		}
		if (tabPlacement != JTabbedPane.LEFT
				&& tabPlacement != JTabbedPane.RIGHT) {
			super.paintIcon(g, tabPlacement, tabIndex, icon, iconRect,
					isSelected);
			return;
		}

		int a = icon.getIconHeight() > icon.getIconWidth() ? icon
				.getIconHeight() : icon.getIconWidth();
		BufferedImage memImage = new BufferedImage(a, a,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = (Graphics2D) memImage.getGraphics();
		AffineTransform at = AffineTransform.getTranslateInstance(a / 2, a / 2);
		// at.rotate(1.5 * Math.PI);
		g2d.transform(at);
		icon.paintIcon(null, g2d, -icon.getIconWidth() / 2, -icon
				.getIconHeight() / 2);

		int dx1 = iconRect.x + (iconRect.width - icon.getIconHeight()) / 2;
		int dy1 = iconRect.y + (iconRect.height - icon.getIconWidth()) / 2;
		int dx2 = dx1 + icon.getIconHeight();
		int dy2 = dy1 + icon.getIconWidth();
		int sx1 = (a - icon.getIconHeight()) / 2;
		int sx2 = sx1 + icon.getIconHeight();
		int sy1 = (a - icon.getIconWidth()) / 2;
		int sy2 = sy1 + icon.getIconWidth();

		g.drawImage(memImage, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, tabPane);
	}

	protected void paintText(Graphics g, int tabPlacement, Font font,
			FontMetrics metrics, int tabIndex, String title,
			Rectangle textRect, boolean isSelected) {
		if (tabPlacement != JTabbedPane.LEFT
				&& tabPlacement != JTabbedPane.RIGHT) {
			super.paintText(g, tabPlacement, font, metrics, tabIndex, title,
					textRect, isSelected);
			return;
		}

		if (title == null || title.equals(""))
			return;

		Graphics2D g2d = (Graphics2D) g;

		g2d.setColor(tabPane.getForeground());
		g2d.setFont(font);
		g2d.drawString(title, textRect.x, textRect.y);

	}

}
